/**


 */
(function (window) {
	/**
	 * д��window����
	 */
	window.mobile = {
		getCityWeather: getCityWeather
	};

	function getCityWeather() {
		var wc = 0;
		if (window.localStorage) {
			if (localStorage.getItem("wc")) {
				wc = localStorage.getItem("wc");
			} else {
				wc = 58362;
				localStorage.setItem('wc', wc);
			}

			$.ajax({
				type: 'GET',
				url: '/index.php?c=index&a=getTianqiInfo&wc=' + wc,
				success: function (json_data) {
					var _wea = eval('(' + json_data + ')');
					$('.city_name').html(_wea.city + '<i class="bor_arrow"></i>');
					$('.temperature').html(_wea.day1.tempLow + _wea.bolang + _wea.day1.tempHigh + _wea.fuhao);
					$('.temperature').attr('href', 'http://waptianqi.2345.com/' + _wea.pinyin + '-' + _wea.id + '.htm');
					$('.temperature').attr('target', '_blank');
					$('.weather').html('<i class="w' + _wea.day1.img + '_l"></i>' + _wea.day1.weather);
					$('.weather').attr('href', 'http://waptianqi.2345.com/' + _wea.pinyin + '-' + _wea.id + '.htm');
					$('.weather').attr('target', '_blank');
				}
			});
		}
	}

	var search = "";
	var search_t = '';
	window.mobile.search = {
		_domain: "m3.2345.com",
		_search: {
			'google': {
				'name': '�ȸ�',
				'url': 'http://www.google.com/m/search',
				'key': 'q',
				'text': '�ȸ�',
				'icon': '/public/pic/google.png'
			},
			'baidu': {
				'name': '�ٶ�',
				'url': 'http://m.baidu.com/s',
				'key': 'word',
				'text': '�ٶ�',
				'icon': '/public/pic/baidu.png'
			},
			'yicha': {
				'name': '�ײ�',
				'url': 'http://yicha.cn/union/u.jsp?p=page&site=2145958760',
				'key': 'key',
				'text': '�ײ�',
				'icon': '/public/pic/yicha.png'
			},
			'easou': {
				'name': '����',
				'url': 'http://ad2.easou.com:8080/j10ad/ea2.jsp?cid=bicn3516_48168_D_1&channel=11',
				'key': 'key',
				'text': '����',
				'icon': '/public/pic/easou.png'
			},
			'yunyun': {
				'name': '����',
				'url': 'http://searchs.yunyun.com/Search.php?refl=msjdhz_0_1_0_0',
				'key': 'q',
				'text': '����',
				'icon': '/public/pic/baidu.png'
			}
		},
		_search_t: '',
		_xhr: null,
		$: function (_id) {
			return document.getElementById(_id);
		},
		_html: function (_id, _html) {
			if (!_html) {
				return this.$(_id).innerHTML;
			} else {
				this.$(_id).innnerHTML = _html;
			}
		},
		_class: function (_id, _class) {
			if (!_class) {
				return this.$(_id).className;
			} else {
				this.$(_id).className = _class;
			}
		},
		_value: function (_id, _value) {
			if (!_value) {
				return this.$(_id).value;
			} else {
				this.$(_id).value = _vlaue;
			}
		},
		_creat: function (_lab) {
			return document.createElement(_lab);
		},
		set_search: function (id) {
			var my_search = eval(this._search);
			this.$("search_icon").src = my_search[id].icon;
			this.$('qi').name = my_search[id].key;
			this.$("search_form").action = my_search[id].url;
			if (id == 'easou') {
				this.$("search_form").method = 'post';
			} else {
				this.$("search_form").method = 'get';
			}
			localStorage.setItem('m_search', id);
		},
		showUrls: function (_id) {
			if ($('#dd_' + _id).css('display') != 'block') {
				$('.all_website').removeClass('more_shrink');
				;
				$('.dd_all_website').hide();
				$('#li_' + _id).addClass('more_shrink');
				$('#dd_' + _id).css('display', 'block');
			} else {
				$('#li_' + _id).removeClass('more_shrink');
				$('#dd_' + _id).css('display', 'none');
			}
		},
		getByClass: function (className, tagName) {
			var all = document.all ? document.all : document.getElementsByTagName('*');
			var elements = new Array();
			for (var e = 0; e < all.length; e++) {
				if (all[e].className == className) {
					elements[elements.length] = all[e];
					// break;
				}
			}
			return elements;
		},
		initXhr: function () {
			if (window.XMLHttpRequest) {
				try {
					this.xhr = new XMLHttpRequest()
				} catch (e) {
					this.xhr = false
				}
			} else if (window.ActiveXObject) {
				try {
					this.xhr = new ActiveXObject("Msxml2.XMLHTTP")
				} catch (e) {
					try {
						this.xhr = new ActiveXObject("Microsoft.XMLHTTP")
					} catch (e) {
						this.xhr = false
					}
				}
			}
		}
	};

})(window);

var topBaiduJson = null;

function ajaxTopBaidu() {
	$.ajax({
		type: 'get',
		url: '/index.php?c=index&a=ajaxTobBaidu',
		async: false,
		success: function (info) {
			var json_info = eval('(' + info + ')');
			topBaiduJson = json_info;
		}
	});
}

var topClick = new Array('hot-bdtop', 'book-bdtop', 'girl-bdtop', '7days-bdtop');
$('#top-baidu-button li').click(function () {
	$('#top-baidu-button li').removeClass('cur');
	$(this).addClass('cur');
	var index = $(this).index();

	var hot = topBaiduJson[index];

	var hot_data = new Array();
	var temp1 = 0,
		temp2 = 5;
	for (i = 0; i < 10; i++) {
		if (i % 2 == 0) {
			hot_data.push('<li><span class="num' + (temp1 < 3 ? ' hot' : '') + '">' + (temp1 + 1) + '</span><a onclick="cc(\'' + topClick[index] + '\')" target="_blank" href="' + hot[temp1]['url'] + (encodeURI(hot[temp1]['name'])) + '" title="' + hot[temp1]['name'] + '" target="_blank">' + hot[temp1]['name'] + '</a></li>');
			temp1++;
		} else {
			hot_data.push('<li><span class="num">' + (temp2 + 1) + '</span><a onclick="cc(\'' + topClick[index] + '\')" target="_blank" href="' + hot[temp2]['url'] + (encodeURI(hot[temp2]['name'])) + '" title="' + hot[temp2]['name'] + '" target="_blank">' + hot[temp2]['name'] + '</a></li>');
			temp2++;
		}
	}

	$('#top-baidu').html(hot_data.join('\n'));
});

var ifengNewsJson = null;
var ifengClick = new Array('top-news', 'soci-news', 'ent-news', 'mil-news');
function ajaxIfengNews() {
	$.ajax({
		type: 'get',
		url: '/index.php?c=index&a=ajaxIfengNews',
		success: function (info) {
			var index = 0;
			var json_info = eval('(' + info + ')');
			ifengNewsJson = json_info;
			console.log(ifengNewsJson);
		}
	});
}

$('#ifeng-news-button li').click(function () {
	$('#ifeng-news-button li').removeClass('cur');
	$(this).addClass('cur');
	var index = $(this).index();

	var toutiao = ifengNewsJson[index];

	var toutiao_data = new Array();

	for (var i = 0; i < 5; i++) {
		toutiao_data.push('<li><a onclick="cc(\'' + ifengClick[index] + '\')" href="' + toutiao[i]['link'] + '" target="_blank" title="' + toutiao[i]['title'] + '">' + toutiao[i]['title'] + '</a></li>');
	}

	$('#ifeng-news').html(toutiao_data.join("\n"));
});

$('#qi').bind('input propertychange', function () {
	var keyword = $('#qi').val();
	if (keyword == '') {
		$('.suggest_wrap').hide();
		$('.i_cross').css('display', 'none');
		return;
	}
	$('.i_cross').css('display', 'block');
	var url = 'http://m.baidu.com/su?p=3&ie=utf-8&from=wise_web&wd=' + keyword + '&t=' + Math.round(new Date().getTime() / 1000);
	loadJs(url);
});

$('body').on('keydown', function (event) {
	if ($.os.ios) {
		if (event.which == 13) {
			$('#search_form')[0].submit();
			setSearchHistory();
			$('.suggest_wrap').hide();
			document.body.scrollTop = 0;
		}
	}
});

$('#search_btn').on('click', function (e) {
	cc('search');
	$('.suggest_wrap').hide();
	document.body.scrollTop = 0;
	if ($('#qi').val() == '') {
		window.open('http://m.baidu.com/?from=1749b');
	} else {
		setSearchHistory();
		$('.suggest_wrap').hide();
		$('#search_form')[0].submit();
	}
})

$('#qi').click(function () {
	setTimeout(function () {
		window.scrollTo(0, $('.search_box').offset().top - 10)
	}, 100);

	var keyword = $('#qi').val();
	if (keyword != '') {
		return;
	}

	if ($('#qi').val() == '' && $('.suggest_wrap').css('display') == 'none') {
		$('#search-data').html('');
		var count = 0;
		if (window.localStorage) {
			if (localStorage.getItem('history')) {
				var history = localStorage.getItem('history');
				var hisArray = history.split(',');

				var search_data = new Array();

				for (var i = 0; i < hisArray.length; i++) {
					count++;
					search_data.push('<li><a onclick="cc(\'search\');cc(\'hisclick-serh\');" href="http://m.baidu.com/s?from=1749b&word=' + hisArray[i] + '" target="_blank">' + hisArray[i] + '</a><div class="op" onclick="cc(\'hisclick-serh\');changeKeyword(\'' + hisArray[i] + '\')"><em></em></div></li>');
				}
				$('#search-data').html(search_data.join('\n'));
				$('.clear_history').show();
			} else {
				$('.clear_history').hide();
			}
		}

		//��ѯ�ٶȷ��ư�
		var total = 7 - count;
		if (total != 0) {
			var top_data = new Array();
			if (topBaiduJson.length) {
				var data = topBaiduJson[0];
				if (localStorage.getItem('history')) {
					var history = localStorage.getItem('history');
					var hisArray = history.split(',');
				}
				var t = 1;
				for (var i = 0; i < data.length; i++) {
					if (hisArray && in_array(data[i]['name'], hisArray)) {
						continue;
					}
					top_data.push('<li><a target="_blank" onclick="cc(\'hotclick-serh\');" href="http://m.baidu.com/s?from=1749b&word=' + data[i]['name'] + '" title="' + data[i]['name'] + '">' + data[i]['name'] + '<i class="icon_txt">��</i></a><div class="op" onclick="cc(\'hotclick-serh\');changeKeyword(\'' + data[i]['name'] + '\')"><em></em></div></li>');

					if (t == total) {
						break;
					}

					t++;
				}
			}

			$('#search-data').html($('#search-data').html() + top_data.join('\n'));
		}

		$('.suggest_wrap').show();
	}
});

$('.clear_history').click(function () {
	cc('hisclean-serh');
	if (confirm('���ȫ����ѯ��ʷ��¼��')) {
		if (window.localStorage) {
			localStorage.removeItem('history');
			$('.suggest_wrap').hide();
		}
	}
});

function think(keyword) {
	var url = 'http://m.baidu.com/su?p=3&ie=utf-8&from=wise_web&wd=' + keyword + '&t=' + Math.round(new Date().getTime() / 1000);
	loadJs(url);
}

window.baidu = {
	sug: function (data) {
		if (data.s.length > 0) {
			var search_data = new Array();

			for (var i = 0; i < data['s'].length; i++) {
				if (i < 7) {
					var content = data['s'][i].replaceAll(data['q'], '<span>' + data['q'] + '</span>');
					search_data.push('<li><a onclick="cc(\'search\')" href="http://m.baidu.com/s?from=1749b&word=' + data['s'][i] + '" target="_blank">' + content + '</a><div class="op" onclick="changeKeyword(\'' + data['s'][i] + '\')"><em></em></div></li>');
				}
			}
			$('#search-data').html(search_data.join(''));
			$('.suggest_wrap').show();
		} else {
			$('.suggest_wrap').hide();
		}
	}
}

function changeKeyword(keyword) {
	$('#qi').val(keyword);
	$('.i_cross').show();
	$('#qi')[0].focus();
	think(keyword);
	setTimeout(function () {
		window.scrollTo(0, $('.search_box').offset().top - 10)
	}, 100);
}

$('.i_cross').click(function () {
	$('.search_inp').val('');
	$('.suggest_wrap').hide();
	$('.i_cross').hide();
});

String.prototype.replaceAll = function (reallyDo, replaceWith, ignoreCase) {
	if (!RegExp.prototype.isPrototypeOf(reallyDo)) {
		return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi" : "g")), replaceWith);
	} else {
		return this.replace(reallyDo, replaceWith);
	}
}

function setSearchHistory() {
	var keyword = $('#qi').val();

	if (window.localStorage) {
		if (localStorage.getItem('history')) {
			var history = localStorage.getItem('history');
			var hisArray = history.split(',');
			if (in_array(keyword, hisArray)) {
				return;
			}
			if (hisArray.length >= 7) {
				hisArray.splice(6, 1);
			}
			history = hisArray.join(',');
			localStorage.setItem('history', keyword + ',' + history);
		} else {
			localStorage.setItem('history', keyword);
		}
	}
}

if (getCookie('user_info')) {
	var username = getCookie('name_ie');
	data = username.split('%');
	if (data.length > 1) {
		uName = '';
		for (i = 0; i < data.length; i++) {
			if (data[i] > 0) {
				uName += String.fromCharCode(data[i]);
			}
		}
	} else {
		uName = data;
	}
	$('.login').attr('href', '/index.php?c=favorite');
	$('.login').html(uName);
}

window.mobile.getCityWeather();
ajaxTopBaidu();
ajaxIfengNews();